<?php

namespace Admin\Services;

/**
 * Description of Service
 *
 * @author nofear
 */
class Tags extends Service {
    
    public function autoSuggest() {
        $q = $this->sanitize($this->getRequest()->get('q'));
        
        $QB = $this->EntityManager->createQueryBuilder();
        
        $QB->select('t')
                    ->from('Shared\Main\Tag', 't')
                    ->andWhere('t.name LIKE :like')
                    ->setParameter('like', '%'.$q.'%')
                    ->orderBy('t.name');
        
        $tags = $QB->getQuery()->getResult();
        $results = array();
        
        if (count($tags)) {
            foreach($tags as $Tag) {
                $results[] = array(
                    'id' => $Tag->getId(),
                    'value' => $Tag->getName()
                );
            }
        } else {
            $results[] = array(
                    'id' => null,
                    'value' => 'No Suggestions'
            );
        }
        
        $response = new \Symfony\Component\HttpFoundation\JsonResponse($results);
        
        return $response;
    }
}
